package com.fr.report.web.ui;

import com.fr.base.ArrayUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONFunction;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.js.JavaScript;
import com.fr.report.web.event.Listener;
import com.fr.web.Repository;

public class MenuButton extends Button
{
  protected Button[] menuItems;

  public MenuButton(String paramString1, String paramString2)
  {
    super(paramString1, paramString2);
  }

  public String getXType()
  {
    return "menubutton";
  }

  public Button[] getMenuItems()
  {
    return this.menuItems;
  }

  public void setMenuItems(Button[] paramArrayOfButton)
  {
    this.menuItems = paramArrayOfButton;
  }

  public Button[] createMenuItems()
  {
    return getMenuItems();
  }

  public JSONObject createJSONConfig(Object paramObject, Repository paramRepository)
    throws JSONException
  {
    JSONArray localJSONArray;
    int i;
    setDisabled(true);
    JSONObject localJSONObject1 = super.createJSONConfig(paramObject, paramRepository);
    Button[] arrayOfButton = createMenuItems();
    if (!(ArrayUtils.isEmpty(arrayOfButton)))
    {
      localJSONArray = new JSONArray();
      localJSONObject1.put("menu", localJSONArray);
      for (i = 0; i < arrayOfButton.length; ++i)
      {
        JSONObject localJSONObject2 = arrayOfButton[i].createChildConfig(paramRepository);
        localJSONArray.put(localJSONObject2);
        Listener[] arrayOfListener = arrayOfButton[i].createListeners(paramRepository);
        for (int j = 0; j < arrayOfListener.length; ++j)
          if (ComparatorUtils.equals("click", arrayOfListener[j].getName()))
          {
            localJSONObject2.put("handler", new JSONFunction(new String[] { "e", "item" }, arrayOfListener[j].getAction().createJS(paramRepository)));
            break;
          }
      }
    }
    return localJSONObject1;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("Widget")))
      ArrayUtils.add(this.menuItems, ReportXMLUtils.readButton(paramXMLableReader));
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    for (int i = 0; (this.menuItems != null) && (i < this.menuItems.length); ++i)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, this.menuItems[i], "Widget");
  }

  public boolean equals(Object paramObject)
  {
    if ((paramObject == null) || (!(paramObject instanceof MenuButton)))
      return false;
    return ((super.equals(paramObject)) && (ComparatorUtils.equals(this.menuItems, ((MenuButton)paramObject).menuItems)));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    int i;
    MenuButton localMenuButton = (MenuButton)super.clone();
    if (this.menuItems != null)
    {
      localMenuButton.menuItems = new Button[this.menuItems.length];
      for (i = 0; i < this.menuItems.length; ++i)
        localMenuButton.menuItems[i] = ((MenuButton)this.menuItems[i].clone());
    }
    return localMenuButton;
  }
}